﻿package com.zkingsoft.services.pc.impl;

import com.zkingsoft.services.pc.PcFoodMaterialService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.pc.PcFoodMaterial;
import com.zkingsoft.model.pc.PcMenuFood;
import com.zkingsoft.model.pc.PcMicroFood;
import com.zkingsoft.dao.pc.PcFoodMaterialDao;
import com.zkingsoft.dao.pc.PcMenuFoodDao;
import com.zkingsoft.dao.pc.PcMicroFoodDao;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-03-14 14:37
 */
@Service("pcFoodMaterialService")
public class  PcFoodMaterialServiceImpl implements    PcFoodMaterialService  {

	
	@Autowired
	private PcFoodMaterialDao pcFoodMaterialDao;
	@Autowired
	private PcMicroFoodDao pcMicroFoodDao;
	@Autowired
	private PcMenuFoodDao pcMenuFoodDao;
	@Autowired
	private ServiceUtil serviceUtil;
	@Transactional
	@Override
	public int add(PcFoodMaterial pcFoodMaterial){
		if (serviceUtil.addCheckRepeat("pc_food_material", "fod_name", pcFoodMaterial.getFodName())) {
			throw new GlobleException("原材料名已经存在");
		}
		int m=pcFoodMaterialDao.insert(pcFoodMaterial);
		List<PcMicroFood> microFood = pcFoodMaterial.getMicroFood();
		PcMicroFood miroFood = new PcMicroFood();
		miroFood.setFodId(pcFoodMaterial.getFodId());
		pcMicroFoodDao.deleteByModel(miroFood);
		
		for(PcMicroFood pmf : microFood ){
			pmf.setFodId(pcFoodMaterial.getFodId());
			pcMicroFoodDao.insert(pmf);
		}
		
		return m;
		
	}
	
	@Override
	public int batchAdd(List<PcFoodMaterial>  pcFoodMaterialList) {
		int num = 0;
		int c = 10000;
		int size = pcFoodMaterialList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= pcFoodMaterialList.size() ? pcFoodMaterialList.size() : end;
			List<PcFoodMaterial> insertList = pcFoodMaterialList.subList(begin, end);
			num += pcFoodMaterialDao.batchInsert(insertList);
			
		}
		for(PcFoodMaterial foodMaterial : pcFoodMaterialList){
			List<PcMicroFood> microFood = foodMaterial.getMicroFood();
			for(PcMicroFood pmf : microFood){
				pmf.setFodId(foodMaterial.getFodId());
				pcMicroFoodDao.batchInsert(microFood);
			}
		}
		return num;
		
	}
	
	
	/**
	 * 新增修改添加微量元素计量数据进入数据库
	 */
    @Override
	public int modifyByMap(PcFoodMaterial oldPcFoodMaterial
	,PcFoodMaterial newPcFoodMaterial){
    	
		List<PcMicroFood> microFood = newPcFoodMaterial.getMicroFood();
		PcMicroFood miroFood = new PcMicroFood();
		miroFood.setFodId(newPcFoodMaterial.getFodId());
		pcMicroFoodDao.deleteByModel(miroFood);
		
		for(PcMicroFood pmf : microFood ){
			pmf.setFodId(newPcFoodMaterial.getFodId());
			pcMicroFoodDao.insert(pmf);
		}
		
		
		if (!ModelUtils.isModified(oldPcFoodMaterial, newPcFoodMaterial)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldPcFoodMaterial, newPcFoodMaterial);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("fodId", oldPcFoodMaterial.getFodId());
			pcFoodMaterialDao.updateByMap(modifyMap);
		}

		
		return 1;
		
	}
	
	@Override
	public int modifyByModel(PcFoodMaterial pcFoodMaterial){
	
		return pcFoodMaterialDao.updateByModel(pcFoodMaterial);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
		PcMenuFood food = new PcMenuFood();
		PcMicroFood miroFood = new PcMicroFood();
		for(Long id:list){
			miroFood.setFodId(id);
			food.setFodId(id);
			pcMenuFoodDao.deleteByModel(food);
			pcMicroFoodDao.deleteByModel(miroFood);
		}
		return pcFoodMaterialDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long fodId){
	
		return pcFoodMaterialDao.deleteById(fodId);
	
	}
	
	@Override
	public int removeByModel(PcFoodMaterial pcFoodMaterial){
	
		return pcFoodMaterialDao.deleteByModel(pcFoodMaterial);
	
	}
	
	
	@Override
	public List<PcFoodMaterial> findInPage(PcFoodMaterial pcFoodMaterial,  PaginationVO pageVo){
	
		return pcFoodMaterialDao.selectInPage(pcFoodMaterial , pageVo);
	
	}
	
	@Override
	public List<PcFoodMaterial> findByModel(PcFoodMaterial pcFoodMaterial){
	
		return pcFoodMaterialDao.selectByModel(pcFoodMaterial);
	
	}
	
	@Override
	public int  findTotal(PcFoodMaterial pcFoodMaterial){
	
		return pcFoodMaterialDao.selectTotalRecord(pcFoodMaterial);
	
	}
	
	@Override
	public PcFoodMaterial  findById(Long fodId){
	
		return pcFoodMaterialDao.selectById(fodId);
	
	}

	@Override
	public List<PcFoodMaterial> selectMenuFood(Long menId) {
		return pcFoodMaterialDao.selectMenuFood(menId);
	}

   	
	
	
}